Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookie Authentication #896

Open
wants to merge 142 commits into
base: develop
Choose a base branch
from
Open

Conversation

handesirikci0rso
Copy link

this pr is to make the changes gradually and under control, to prevent giant modifications at once. also some comments will be added for documentation purposes.

options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
})
.AddIdentityCookies();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

configuring authentication scheme and cookies is necessary since using AddIdentityCore requires more direct control to the setup

If we were using AddIdentity then authentication schemes and cookies would be configured automatically without a need for these initialisations


if (_identityConfiguration.UseCookies)
{
result = await _signInManager.PasswordSignInAsync(user, request.Password, false, true);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

signInManager.PasswordSignInAsync() can only be used with cookies

Copy link
Member

@VILLAN3LL3 VILLAN3LL3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed the non-test-files in the first attempt. I will review the tests when the comments in the non-test-files are resolved

.gitignore Outdated
.dccache
Orso.Arpa.Api/__azurite_db_blob__.json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can get that out of here. You can configure where azurite stores the data and this should not necessarily be in the project folder.

@@ -30,13 +30,15 @@ public AuthController(IAuthService authService)
/// <response code="422">If validation fails</response>
[AllowAnonymous]
[HttpPost("login")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status200OK)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the endpoint returns No Content, we should remove the Status200OK attribute

@@ -206,6 +209,7 @@ public async Task<ActionResult<TokenDto>> RefreshAccessToken()
public async Task<ActionResult> Logout()
{
await _authService.RevokeRefreshTokenAsync(RefreshToken, RemoteIpAddress);
await _authService.SignOut();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the controller should be as dump as possible and only call one single service method. The service then acts as an orchestrator and can send multiple commands or queries, if required

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Orso.Arpa.Api.Extensions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not an extension class, is it? Then it should be moved to another appropriate place

@@ -452,6 +465,27 @@ private void ConfigureAuthentication(IServiceCollection services)
.AddRoleManager<RoleManager<Role>>()
.AddUserManager<ArpaUserManager>();

JwtConfiguration jwtConfig = AddConfiguration<JwtConfiguration>(services);

_ = identityBuilder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "RegisterServices" is not the appropriate method for this configuration.
Plus: We now have "services.AddAuthentication" twice in the Startup class. There should be only one authentication configuration

@@ -6,6 +7,8 @@ namespace Orso.Arpa.Domain.General.Interfaces
{
public interface IJwtGenerator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this one is not used for jwt anymore it should get a new and more appropriate name

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TokenGenerator? AuthenticationHelper?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes for example. I think the name of the second method is misleading, as it does not Get the claims identity from a user object, but it creates a claims identity object with data from the user object. Maybe you find a better name for it?

Orso.Arpa.Infrastructure/Authentication/JwtGenerator.cs Outdated Show resolved Hide resolved
Orso.Arpa.Infrastructure/Authentication/JwtGenerator.cs Outdated Show resolved Hide resolved
@@ -794,7 +794,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder)
CreatedAt = new DateTime(2021, 6, 16, 15, 30, 19, 324, DateTimeKind.Local).AddTicks(7866),
CreatedBy = "LocalizationSeedData",
Deleted = false,
Key = "Invalid token supplied",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migration files are automatically generated by entity framework and must not be updated manually - they are already executed on the prod database!
Please update the files in the "Translation" folder and execute a new migration instead (see how-to in readme file of the persistence project)

@@ -1,7 +1,7 @@
[
{
"Key": "This request requires a valid JWT access token to be provided",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the files in the Localization folder are generated automatically during migration and must not be updated manually (see readme file in this folder)

Copy link

sonarcloud bot commented Apr 23, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
2 New issues
2 New Code Smells (required ≤ 0)

See analysis details on SonarCloud

Catch issues before they fail your Quality Gate with our IDE extension SonarLint

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants